####################################################################
## author:    Robert A. Huber, Lukas P. Fesenfeld & Thomas Bernauer
## contact:   robert.huber@ir.gess.ethz.ch
## file name: erc_popcli_recode2.R
## Context:   ERC Populism Project on Public Support for Climate Politics
## started:   2018-03-02
## Summary:   read in csv file and recode
##############################################

originalDF <-read.csv("./original data/original_data.csv", sep = ",", dec = ".")
#note: Was not able to open the original file. Opening it with excel and saving it again
# worked fine.

id <- 1:nrow(originalDF)

df <- data.frame(id)

rm(id)

df$gender <- originalDF$resp_gender

df$age <- as.numeric(sub("_", "", originalDF$resp_age))

df$urbanRural <- factor(originalDF$MKTsize_Recode, 
                        levels = c("_1", "_2"), 
                        labels = c("Urban", "Rural"))

df$region <- factor(originalDF$qRegion2,
                    levels = c("_1", "_2", "_3", "_4"),
                    labels = c("Northeast", "Midwest", "South", "West"))

df$educGrp <- factor(originalDF$EducationRecode,
                     levels = c("_1", "_2", "_3"),
                     labels = c("No College", "Some College", "College + "))

df$inc <- factor(originalDF$Q2_5,
                 levels = c("_1", "_2", "_3", "_4", "_5", "_6"),
                 labels = c("Under $22,500", "$22,501 - $43,500", "$43,501- $72,000", "$72,001- $117,000", "$117,001 - $214,500", "More than $214,500"))

df$employ <- factor(originalDF$Q2_10,
                    levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8"),
                    labels = c("Working full time as paid employee", "Working part time as paid employee", "Working as self-employed", "Student", "Unemployed", "Retired", "Disabled", "Other"))

df$employGrp <- ifelse(df$employ != levels(df$employ)[1] & df$employ != levels(df$employ)[2] & df$employ != levels(df$employ)[3], "Not working", "Working")

df$ccKnow <- factor(originalDF$Q2_11,
                    levels = c("_1", "_2", "_3"),
                    labels = c("Yes", "To some extent", "No"))

df$pop1 <- factor(originalDF$Q3_1.._1..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop1_num <- abs(as.numeric(df$pop1)-8)

df$pop2 <- factor(originalDF$Q3_1.._2..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop2_num <- as.numeric(df$pop2)

df$pop3 <- factor(originalDF$Q3_1.._3..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop3_num <- abs(as.numeric(df$pop3)-8)

df$pop4 <- factor(originalDF$Q3_1.._4..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop4_num <- abs(as.numeric(df$pop4)-8)

df$pop5 <- factor(originalDF$Q3_1.._5..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop5_num <- as.numeric(df$pop5)

df$pop6 <- factor(originalDF$Q3_1.._6..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop6_num <- abs(as.numeric(df$pop6)-8)

df$pop7 <- factor(originalDF$Q3_1.._7..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop7_num <- abs(as.numeric(df$pop7)-8)

df$pop8 <- factor(originalDF$Q3_1.._8..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop8_num <- as.numeric(df$pop8)

df$pop9 <- factor(originalDF$Q3_1.._9..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly agree", "Agree", "Somewhat agree", "Neither disagree nor agree", "Somewhat disagree", "Disagree", "Strongly disagree"))

df$pop9_num <- abs(as.numeric(df$pop9)-8)

df$trt <- factor(originalDF$Group_Texts,
                 levels = c("_1", "_2", "_3", "_4", "_5", "_6"),
                 labels = c("Elite Cares Climate", "Elite Cares Neutral", "Elite Self-Interest Climate", "Elite Self-Interest Neutral", "Control w/ PA", NA))

df<- within(df, trt <- relevel(trt, ref = "Control w/ PA"))

df$cliEco <- abs(as.numeric(sub("_", "", originalDF$Q5_1))-8)

df$cliAct <- factor(originalDF$Q5_2,
                    levels = c("_1", "_2", "_3", "_99"),
                    labels = c("Too much", "About the right amount", "Not enough", "Don't know"))

df<- within(df, cliAct <- relevel(cliAct, ref = "Too much"))

df$wtp1 <- factor(originalDF$Q6_1.._1..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$wtp1_num <- as.numeric(df$wtp1)

df$wtp2 <- factor(originalDF$Q6_1.._2..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$wtp2_num <- as.numeric(df$wtp2)

df$wtp3 <- factor(originalDF$Q6_1.._3..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$wtp3_num <- as.numeric(df$wtp3)

df$wtp4 <- factor(originalDF$Q6_1.._4..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$wtp4_num <- as.numeric(df$wtp4)

df$wtp5 <- factor(originalDF$Q6_1.._5..._scale,
                  levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                  labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$wtp5_num <- as.numeric(df$wtp5)

df$wtpsum <- (df$wtp1_num + df$wtp2_num + df$wtp3_num + df$wtp4_num + df$wtp5_num)/5

df$cliConcern1 <- factor(originalDF$Q9_1.._1..._scale,
                         levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                         labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$cliConcern1_num <- as.numeric(df$cliConcern1)

df$cliConcern2 <- factor(originalDF$Q9_1.._2..._scale,
                         levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                         labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$cliConcern2_num <- as.numeric(df$cliConcern2)

df$cliConcern3 <- factor(originalDF$Q9_1.._3..._scale,
                         levels = c("_1", "_2", "_3", "_4", "_5", "_6", "_7"),
                         labels = c("Strongly disagree", "Disagree", "Somewhat disagree", "Neither disagree nor agree", "Somewhat agree", "Agree", "Strongly agree"))

df$cliConcern3_num <- as.numeric(df$cliConcern3)

df$partyID <- factor(originalDF$Q13_1,
                     levels = c("_1", "_2", "_3", "_4", "_97"),
                     labels = c("D", "R", "I", "Something else", "Prefer not to answer"))

df$partyID_oth <- originalDF$Q13_1._other

df$lr_factor <- factor(originalDF$Q13_3.._1..._scale,
                       levels = c("_0", "_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8", "_9", "_10"),
                       labels = c("Left", 1:9, "Right"))

df$lr_num <- as.numeric(df$lr_factor)

rm(list=setdiff(ls(), c("df")))
